package xyz.yuzegod.megawalls.classes.normal.puppet;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.entity.Entity;
import org.bukkit.entity.FallingBlock;
import org.bukkit.entity.Player;
import org.bukkit.metadata.MetadataValue;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.Vector;
import xyz.yuzegod.megawalls.MegaWalls;
import xyz.yuzegod.megawalls.classes.Classes;
import xyz.yuzegod.megawalls.classes.Skill;
import xyz.yuzegod.megawalls.game.GamePlayer;
import xyz.yuzegod.megawalls.stats.KitStatsContainer;
import xyz.yuzegod.megawalls.util.ParticleEffect;
import xyz.yuzegod.megawalls.util.PlayerUtils;

import java.util.ArrayList;
import java.util.List;

public class MainSkill extends Skill
{
    public MainSkill(final Classes classes) {
        super("钢铁之击", classes);
    }
    
    @Override
    public int maxedLevel() {
        return 5;
    }
    
    @Override
    public double getAttribute(final int level) {
        switch (level) {
            case 1: {
                return 2.6;
            }
            case 2: {
                return 3.2;
            }
            case 3: {
                return 3.8;
            }
            case 4: {
                return 4.4;
            }
            case 5: {
                return 5.0;
            }
            default: {
                return 2.6;
            }
        }
    }
    
    @Override
    public List<String> getInfo(final int level) {
        final List<String> lore = new ArrayList<String>();
        if (level == 1) {
            lore.add(" §8▪ §7在4.5块方块半径内");
            lore.add("   §7的区域造成§a" + this.getAttribute(level) + "§7点伤害。");
            lore.add(" ");
            lore.add("§7冷却时间:§a2秒");
            return lore;
        }
        lore.add(" §8▪ §7在4.5块方块半径内");
        lore.add("   §7的区域造成§8" + this.getAttribute(level - 1) + " ➜ §a" + this.getAttribute(level) + "§7点伤害。");
        return lore;
    }
    
    @Override
    public void upgrade(final GamePlayer gamePlayer) {
        final KitStatsContainer kitStats = gamePlayer.getPlayerStats().getKitStats(this.getClasses());
        kitStats.addSkillLevel();
    }
    
    @Override
    public int getPlayerLevel(final GamePlayer gamePlayer) {
        return gamePlayer.getPlayerStats().getKitStats(this.getClasses()).getSkillLevel();
    }
    
    @Override
    public boolean use(final GamePlayer gamePlayer, final KitStatsContainer kitStats) {
        final Player player = gamePlayer.getPlayer();
        if (Puppet.skillCooldown.getOrDefault(gamePlayer, 0) > 0) {
            player.sendMessage("§c技能正在冷却中！");
            return false;
        }
        final List<Player> players = this.getNearbyPlayers(player, 4.5);
        if (players.isEmpty()) {
            player.sendMessage("§c没有玩家在目标范围内！");
            return false;
        }
        final Location location = player.getLocation().add(0.0, 4.0, 0.0);
        ParticleEffect.SMOKE_NORMAL.display(0.0f, 0.0f, 0.0f, 1.0f, 20, location, 10.0);
        location.getWorld().playSound(location, Sound.IRONGOLEM_DEATH, 1.0f, 0.0f);
        final List<FallingBlock> fallingBlocks = new ArrayList<FallingBlock>();
        final FallingBlock fb = location.getWorld().spawnFallingBlock(location, Material.IRON_BLOCK, (byte)0);
        final FallingBlock fb2 = location.getWorld().spawnFallingBlock(location, Material.IRON_BLOCK, (byte)0);
        final FallingBlock fb3 = location.getWorld().spawnFallingBlock(location, Material.IRON_BLOCK, (byte)0);
        final FallingBlock fb4 = location.getWorld().spawnFallingBlock(location, Material.IRON_BLOCK, (byte)0);
        final FallingBlock fb5 = location.getWorld().spawnFallingBlock(location, Material.IRON_BLOCK, (byte)0);
        final FallingBlock fb6 = location.getWorld().spawnFallingBlock(location, Material.IRON_BLOCK, (byte)0);
        final FallingBlock fb7 = location.getWorld().spawnFallingBlock(location, Material.IRON_BLOCK, (byte)0);
        final FallingBlock fb8 = location.getWorld().spawnFallingBlock(location, Material.IRON_BLOCK, (byte)0);
        fallingBlocks.add(fb);
        fallingBlocks.add(fb2);
        fallingBlocks.add(fb3);
        fallingBlocks.add(fb4);
        fallingBlocks.add(fb5);
        fallingBlocks.add(fb6);
        fallingBlocks.add(fb7);
        fallingBlocks.add(fb8);
        final double vector = 0.4;
        final double vector2 = 0.6;
        fb.setVelocity(new Vector(vector, 0.0, 0.0));
        fb2.setVelocity(new Vector(0.0, 0.0, vector));
        fb3.setVelocity(new Vector(-vector, 0.0, 0.0));
        fb4.setVelocity(new Vector(0.0, 0.0, -vector));
        fb5.setVelocity(new Vector(vector2, 0.0, vector2));
        fb6.setVelocity(new Vector(-vector2, 0.0, vector2));
        fb7.setVelocity(new Vector(vector2, 0.0, -vector2));
        fb8.setVelocity(new Vector(-vector2, 0.0, -vector2));
        for (final FallingBlock fallingBlock : fallingBlocks) {
            fallingBlock.setMetadata(MegaWalls.getMetadataValue(), (MetadataValue)MegaWalls.getFixedMetadataValue());
            fallingBlock.setDropItem(false);
        }
        new BukkitRunnable() {
            public void run() {
                if (!player.isOnline() || fb.isOnGround()) {
                    for (final FallingBlock fallingBlock : fallingBlocks) {
                        fallingBlock.remove();
                    }
                    this.cancel();
                }
            }
        }.runTaskTimer((Plugin)MegaWalls.getInstance(), 10L, 1L);
        for (final Player other : players) {
            final GamePlayer gameOther = GamePlayer.get(other.getUniqueId());
            if (gamePlayer.getGameTeam().isInTeam(gameOther)) {
                continue;
            }
            ParticleEffect.FLAME.display(0.0f, 0.0f, 0.0f, 1.0f, 5, other.getLocation(), 10.0);
            other.damage(this.getAttribute(kitStats.getSkillLevel()), (Entity)player);
            gameOther.setLastDamage(gamePlayer, System.currentTimeMillis());
        }
        Puppet.skillCooldown.put(gamePlayer, 2);
        return true;
    }
    
    @Override
    public String getSkillTip(final GamePlayer gamePlayer) {
        return this.getClasses().getNameColor() + "§l" + this.getName() + " " + ((Puppet.skillCooldown.getOrDefault(gamePlayer, 0) == 0) ? ((gamePlayer.getEnergy() == 100) ? "§a§l✓" : "§c§l✕") : ("§c§l" + Puppet.skillCooldown.get(gamePlayer) + "秒"));
    }
    
    private List<Player> getNearbyPlayers(final Player player, final double radius) {
        final List<Player> players = new ArrayList<Player>();
        for (final Player other : PlayerUtils.getNearbyPlayers((Entity)player, radius)) {
            final GamePlayer gameOther = GamePlayer.get(other.getUniqueId());
            if (!gameOther.isSpectator()) {
                if (GamePlayer.get(player.getUniqueId()).getGameTeam().isInTeam(gameOther)) {
                    continue;
                }
                players.add(other);
            }
        }
        return players;
    }
}
